#!/usr/bin/python

import os
import pexpect
import sys


path = "../src/"

cmd = "cscore"
failed = 0
passed = 0


# Takes a list of strings and returns a string
def flatten(list):
    result = ""
    for e in list:
        result += e
    return result

# Test program
print "\nTest suite for cscore\n"
print "Testing program interaction...\n"
for test in os.listdir("input"):
    if os.path.isfile("input/"+test):
        sys.stdout.write("    Testing case "+test+"... ")
        sys.stdout.flush

        # Spawn new cscore and initiate output list
        p = pexpect.spawn(os.path.abspath(path)+'/'+cmd)
        p.setecho(False)
        pout = list()

        # Loop through commands from the input file
        for input in file("input/"+test):
            p.expect('>> ')
            p.sendline(input.strip())
            # Append output (all, even the matched line) and remove \r
            pout.append(p.before.replace("\r","")+p.after+input)

        # Read expected output file
        output = file("output/"+test).readlines()

        # Test if the output is what we expected
        if flatten(pout) == flatten(output):
            print "passed."
            passed += 1
        else:
            print "failed."
            failed += 1

print "\n\nTesting results\n"
print "    Tests passed: "+str(passed)
print "    Tests failed: "+str(failed)+"\n"
